import numpy as np
import pandas as pd

from matplotlib import pyplot as plt
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

diabetes = load_diabetes()
data = diabetes['data']
target = diabetes['target']
feature_names = diabetes['feature_names']
df = pd.DataFrame(data, columns=feature_names)

train_X, test_X, train_Y, test_Y = train_test_split(data, target, train_size=0.8)

'''
linear: 线性核函数
poly: 多项式核函数
rbf: 径像核函数/高斯核
sigmoid: sigmoid核函数
precomputed: 核矩阵
'''
model = SVC(kernel='rbf')
model.fit(train_X, train_Y)

# 手算均方差
y_pred = model.predict(test_X)
sum_mean = 0
for i in range(len(y_pred)):
    sum_mean += (y_pred[i] - test_Y[i]) ** 2
sum_erro = np.sqrt(sum_mean / len(y_pred))  # 测试级的数量

print("RMSE by hand:", sum_erro)

# sklearn自带的评估
score = model.score(test_X, test_Y)
print("sklearn score: {}%".format(score * 100))
